package com.vhall.component.service.room.event.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author jingwen.li
 * @date 2023/2/16
 */
@Component
public class HandlerContext {
    @Autowired
    private ApplicationContext beanFactory;

    /**
     * 获取事件处理器实例
     *
     * @param event 事件
     * @return AbstractEventHandler子类
     */
    public AbstractEventHandler getInstance(String event) {

        Map<String, Class> map = (Map<String, Class>) beanFactory.getBean(EventHandlerType.class.getName());
        Class aClass = map.get(event);
        if (aClass != null) {
            return (AbstractEventHandler) beanFactory.getBean(aClass);
        }
        return null;
    }
}
